package ysq.xyg.host.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.model.po.HostArea;
import ysq.xyg.host.model.po.HostItem;
import ysq.xyg.host.model.vo.HostAreaVo;
import ysq.xyg.host.model.vo.HostItemVo;
import ysq.xyg.host.service.HostAreaService;
import ysq.xyg.host.service.HostItemService;
import ysq.xyg.sys.model.po.SysUser;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 房产模块-小区信息controller层
 *
 * @author shengQiang yu
 * @date 2019-7-1 15:25:38
 */
@RestController
public class HostItemController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(HostItemController.class);
    @Resource
    private HostItemService hostItemService;
    @Resource
    private HostAreaService hostAreaService;
    private HostItem hostItem;

    /**
     * 分页查询
     *
     * @param hostItemVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-7-1 15:25:38
     */
    @BaseLog(module = "房产模块",className = "小区信息",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/hostItem/page",method = RequestMethod.POST)
    public ReturnEntity page(HttpServletRequest request,HostItemVo hostItemVo, int pageSize, int pageNum){
        SysUser sysUser=acceptCurrentUser(request);
        //用户类型：1、总权限管理员，2、区域负责管理员
        if("2".equals(sysUser.getUserType())){
            List<HostArea> hostAreaList=hostAreaService.acceptList(new HostAreaVo("",sysUser.getId()));
            if(hostAreaList!=null&&hostAreaList.size()>0){
                String[] areas=new String[hostAreaList.size()];
                for(int i=0;i<hostAreaList.size();i++){
                    areas[i]=hostAreaList.get(i).getId();
                }
                hostItemVo.setAreaIds(areas);
            }else{
                //当用户没有时，给用户设置区域为不存在的数据
                hostItemVo.setAreaIds(new String[]{"xyg12212121"});
            }
        }
        ReturnEntity returnEntity = hostItemService.page(hostItemVo,pageSize,pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-7-1 15:25:38
     */
    @RequestMapping(value = "/hostItem/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(HttpServletRequest request,String id){
        SysUser sysUser=acceptCurrentUser(request);
        ModelAndView view = new ModelAndView("/host/hostItemUpdateOrSave");
        //用户类型：1、总权限管理员，2、区域负责管理员
        if("2".equals(sysUser.getUserType())){
            List<HostArea> hostAreaList=hostAreaService.acceptList(new HostAreaVo("",sysUser.getId()));
            if(hostAreaList!=null&&hostAreaList.size()>0){
                String areaIds="";
                for(int i=0;i<hostAreaList.size();i++){
                    areaIds+=hostAreaList.get(i).getId()+",";
                }
                view.addObject("areaIds",areaIds);
            }
        }
        view.addObject("sysUser", sysUser);
        if(StrUtil.isNotEmpty(id)){
            hostItem = hostItemService.selectByPrimaryKey(id);
            if(hostItem!=null&&StrUtil.isNotEmpty(hostItem.getAreaId())){
                HostArea hostArea=hostAreaService.selectByPrimaryKey(hostItem.getAreaId());
                hostItem.setAreaName(hostArea.getName());
            }
            view.addObject("hostItem", hostItem);
            view.addObject("openTime", DateUtil.formatDate(hostItem.getOpenTime()));
        }
        return view;
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-7-1 15:25:38
     */
    @BaseLog(module = "房产模块",className = "小区信息",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/hostItem/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        return hostItemService.delete(id,acceptCurrentUser(request));
    }

    /**
     * 添加或修改
     *
     * @param hostItemVo Vo对象
     * @author shengQiang yu
     * @date 2019-7-1 15:25:38
     */
    @BaseLog(module = "房产模块",className = "小区信息",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/hostItem/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request,HostItemVo hostItemVo){
        return hostItemService.addOrUpdate(hostItemVo,acceptCurrentUser(request));
    }
}